#include "AndroidWindow.h"

AndroidWindow::AndroidWindow(const WindowSetup& setup) 
	: m_setup(setup) {

    if (setup.Width > setup.Height)
    {
        changeScreenOrientation();
    }
}

AndroidWindow::~AndroidWindow() {
}

WND_STATUS AndroidWindow::ProcessMessage() const {
	return WND_OK;
}

void AndroidWindow::changeScreenOrientation() {
    if (m_pApp == NULL) {
        return;
    }

    JNIEnv *env;
    m_pApp->activity->vm->AttachCurrentThread(&env, 0);

    jobject me = m_pApp->activity->clazz;
    jclass acl = env->GetObjectClass(me);

    jmethodID miid = env->GetMethodID(acl, "setRequestedOrientation", "(I)V");
    env->CallVoidMethod(me, miid, 0);

    m_pApp->activity->vm->DetachCurrentThread();
}

inline uint32_t AndroidWindow::GetWidth() const {
	return setup.Width;
}

inline uint32_t AndroidWindow::GetHeight() const {
	return setup.Height;
}

inline WND_HANDLE AndroidWindow::GetWindowHandle() const {
    return m_pApp == NULL ? 0 : (WND_HANDLE)m_pApp->window;
}

IEngineWindow* CreateWindowE(const WindowSetup& setup)
{
	return new AndroidWindow(setup);
}